package com.gxa.medical.controller;

import com.gxa.medical.mapper.DoctorTalkMapper;
import com.gxa.medical.pojo.Doctor;
import com.gxa.medical.pojo.Talk;
import com.gxa.medical.pojo.User;
import com.gxa.medical.service.DoctorService;
import com.gxa.medical.service.DoctorTalkService;
import com.gxa.medical.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.swing.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Dusen
 * @date 2022/6/8 16:53
 */
@RestController
@RequestMapping("/doctorTalk")
@Api(value = "医生端的病例探讨相关的api接口")
public class DoctorTalkController {
    @Autowired
    private DoctorTalkService  doctorTalkService;

    @Autowired
    private DoctorService doctorService;

    /**
     * 添加病例探讨表的方法
     * @param //talkName 探讨主题
     * @param //sigStaTime    探讨报名开始时间
     * @param //sigOverTime   探讨报名结束时间
     * @param //staTime   探讨开始时间
     * @param //conTime   探讨持续时间(单位为分钟)
     * @param //money 探讨费用
     * @param //details   探讨说明
     * @return  返回map给前端
     */
    @ApiOperation(value = "添加病例探讨表的方法")
    @PostMapping("/add")
    /*public Map addTalk(String talkName, String sigStaTime, String sigOverTime,
                       String staTime, String conTime, String money, Spring details){*/
    public Map addTalk(@RequestBody Talk talk){

        // 先添加病例探讨表必填项,返回该新增表的自增长ID值
        Integer lines = doctorTalkService.addTalk(talk);
        // 获取该新增表的自增长ID值
        Integer talkId = talk.getTalk_id();


        // 必须探讨表添加成功后才可以进行关联表中的数据的添加

        // TODO 先获取参会医生的id,保存在数组里(目前先模拟一个数组,后面是前端传过来的数组)
        Integer[] inviteDoc = {1002,1003};

        // 要确认影响行数不为0才开始添加关联表中
        if(lines != 0){
            // 开始添加id到关联表中  循环添加
            for (int i = 0; i < inviteDoc.length; i++) {
                doctorTalkService.addInviteDoc(inviteDoc[i],talkId);
            }

            // 将主持人医生id添加到关联表中
            // TODO 获取主持人医生的id (目前先模拟,后面是前端传过来我们查询)
            Integer docId = 1001;
            doctorTalkService.addDocTalk(docId,talkId);

            // 将患者id添加到关联表中
            // TODO 获取患者的id (目前先模拟,后面是前端传过来我们查询)
            Integer userId = 1;
            doctorTalkService.addUserTalk(userId,talkId);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(talkId);

        return jsonResult.getResultMap();
    }


    /**
     * 根据医院查询该医院所有医生的方法(显示医生的职称和名字)
     * @return  返回map给前端
     */
    @PostMapping("/selectDocAllByHos")
    @ApiOperation(value = "根据医院查询该医院所有医生的方法(显示医生的职称和名字)")
    public Map selectDocAllByHos(){
        //TODO 模拟一个医院id 后面是前端传过来医院id
        Integer hosId = 1;
        List<Doctor> docAll = doctorTalkService.selectDocAllByHos(hosId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(docAll);

        return jsonResult.getResultMap();
    }


    /**
     * 根据手机号查询该医生的方法(显示医生的职称和名字以及所属医院)
     * @param phone 该医生的手机号
     * @return  返回map给前端
     */
    @PostMapping("/selectDocAllByPhone")
    @ApiOperation(value = "根据手机号查询该医生的方法(显示医生的职称和名字以及所属医院)")
    @ApiImplicitParam(name = "phone",value = "医生手机号",paramType = "query")
    public Map selectDocAllByPhone(String phone){
        // TODO 模拟一个手机号 (后面是前端将手机号传过来)
        Doctor doctor = doctorTalkService.selectDocAllByPhone(phone);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(doctor);

        return jsonResult.getResultMap();
    }


    /**
     * 查询所有患者的信息  显示患者的名称,性别,年龄,发作类型,关联日期
     * @return 返回map给前端
     */
    // TODO 还没有查询到  患者的"发作类型,关联日期"字段
    @PostMapping("/selectUserAll")
    @ApiOperation(value = "查询所有患者的信息  显示患者的名称,性别,年龄,发作类型,关联日期")
    public Map selectUserAll(){
        List<User> users = doctorTalkService.selectUserAll();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(users);

        return jsonResult.getResultMap();
    }


    /**
     * 根据病例探讨表的id 查询对应的探讨表详情 显示(探讨主题,开始时间,持续时间,费用,患者名称)
     * @param  talkId 病例探讨表的id
     * @return 返回map给前端
     */
    @PostMapping("/selectTalk")
    @ApiOperation(value = "根据病例探讨表的id 查询对应的探讨表详情 显示(探讨主题,开始时间,持续时间,费用,患者名称)")
    public Map selectTalk(@RequestBody Integer talkId){
        /*// TODO 模拟一个病例探讨表的id (后面是前端传过来)
        Integer talkId = 4;*/
        Talk talk = doctorTalkService.selectTalk(talkId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(talk);

        return jsonResult.getResultMap();
    }



    /**
     * 将 报名医生的id 添加进 报名医生-talk表的关联表
     * @param docId     报名医生的id
     * @param talkId    报名的病例探讨表的id
     * @return  返回map给前端
     */
    @PostMapping("/addSignTalk")
    @ApiOperation(value = "将 报名医生的id 添加进 报名医生-talk表的关联表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "docId",value = "报名医生的id"),
            @ApiImplicitParam(name = "talkId",value = "报名的病例探讨表的id")
    })
    public Map addSignTalk(Integer docId,Integer talkId){
        doctorTalkService.addSignTalk(docId,talkId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("报名成功");

        return jsonResult.getResultMap();

    }



    /**
     * 将talk表中的people字段 + 1
     * @param talkId    探讨表的id
     * @return 返回map给前端
     */
    @PostMapping("/updateTalkPeople")
    @ApiOperation(value = "将talk表中的people字段 + 1")
    @ApiImplicitParam(name = "talkId",value = "探讨表的id")
    public Map updateTalkPeople(Integer talkId){
        doctorTalkService.updateTalkPeople(talkId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");

        return jsonResult.getResultMap();
    }




    /**
     * 查询所有病例探讨表
     * @return  返回map给前端
     */
    @PostMapping("/selectAllTalk")
    @ApiOperation(value = "查询所有病例探讨表")
    public Map selectAllTalk(){
        List<Talk> talks = doctorTalkService.selectAllTalk();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(talks);

        return jsonResult.getResultMap();
    }


    /**
     * 查询该医生所有有参与的病例探讨表(主持人/邀请嘉宾/报名的)
     * @param docId 该医生的id
     * @return 返回map给前端
     */
    @PostMapping("/selectDocTalk")
    @ApiOperation(value = "查询该医生所有有参与的病例探讨表(主持人/邀请嘉宾/报名的)")
    @ApiImplicitParam(name = "docId",value = "医生id")
    public Map selectDocTalk(Integer docId){
        List<Talk> talks = doctorTalkService.selectDocTalk(docId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(talks);
        return jsonResult.getResultMap();
    }





}
